package com.example.demo.service.impl;

import com.example.demo.bean.Order;
import com.example.demo.common.User;
import com.example.demo.service.Demo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@Service
public class DemoImpl implements Demo {


    @Override
    public void testThread() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add(i);
        }
        this.getThread(list);
    }

    @Override
    @Cacheable(value = "getUserById", key = "#id")
    public User getUserById(String id) {
        User user = User.builder()
                .userId(id)
                .userName("张三").build();

        return user;
    }


    @Override
    public User getUser() {
        User user = User.builder()
                .userId("456")
                .userName("王二")
                .terminalInfo("ios").build();

        return user;
    }


    @Async("serviceExecutor")
    public void getThread(List<Integer> list) {
        for (Integer in : list) {
            System.out.println(in);
        }
    }


    @Deprecated
    public static void forkJoinTest() {
        List<Order> orders = new ArrayList<>();
        Order order = null;
        ForkJoin forkJoin = null;
        for (int i = 0; i < 100; i++) {
            order = Order.builder().paymentType("123").build();
            orders.add(order);
            forkJoin = new ForkJoin(0, orders.size(), order);
        }
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(forkJoin);

        System.out.println(orders);
    }

    public static void main(String[] args) {
        forkJoinTest();
    }

}
